package jgl.vajra.kingkong.part.login.viewModel;

import android.text.TextUtils;

import com.orhanobut.logger.Logger;

import java.util.Map;

import jgl.vajra.kingkong.bean.LoginInfoBean;
import jgl.vajra.kingkong.part.login.contract.LoginContract;
import jgl.vajra.kingkong.part.login.model.LoginModel;
import jgl.vajra.kingkong.widget.bean.BaseRequestData;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateModel;
import jgl.vajra.kingkong.widget.retrofithelper.rxsubscriber.ProgressObserver;
import jgl.vajra.kingkong.widget.utils.ToastUtils;

@CreateModel(LoginModel.class)
public class LoginViewModel extends LoginContract.ViewModel {
    @Override
    public void getAESSecretKey() {
        mModel.getAESSecretKey().subscribe(new ProgressObserver<String>(false, null) {
            @Override
            public void _onNext(String secretKey) {
                mView.returnAESSecretKeySuccess(secretKey);
            }

            @Override
            public void _onError(String errorMessage, int errorCode) {
                super._onError(errorMessage, errorCode);
                Logger.d("访问失败" + errorMessage);
            }
        });
    }

    @Override
    public void getVerifyCode(Map<String, Object> map) {
        mModel.getVerifyCode(map).subscribe(new ProgressObserver<BaseRequestData<String>>(false, null) {
            @Override
            public void _onNext(BaseRequestData<String> data) {
                ToastUtils.showShort(TextUtils.isEmpty(data.data)?data.msg:data.data);
                mView.returnVerifyCodeData(data.code);
            }

            @Override
            public void _onError(String errorMessage, int errorCode) {
                super._onError(errorMessage, errorCode);
                ToastUtils.showShort(errorMessage);
                mView.returnVerifyCodeData(errorCode);
            }
        });
    }

    @Override
    public void onLogin(Map<String, Object> map) {
        mModel.onLogin(map).subscribe(new ProgressObserver<LoginInfoBean>(true, null) {
            @Override
            public void _onNext(LoginInfoBean data) {
                mView.returnLoginPasswordSuccess(data);
            }

            @Override
            public void _onError(String errorMessage, int errorCode) {
                super._onError(errorMessage, errorCode);
            }
        });
    }

}
